package org.p2psimulator.entity.reputation.gnutellabased;

import org.p2psimulator.entity.Id;
import org.p2psimulator.entity.reputation.Reputation;

public class BayesianReputation extends Reputation {


        private int minInteractions = 20;

	public BayesianReputation(double numplus, double numminus, Id rater,
			Id ratee) {
		super(numplus, numminus, rater, ratee);
	}

	protected double alfa = 0;

	protected double beta = 0;

	@Override
	public double getAverageReputation(){
		return expectation();
            //return super.getAverageReputation();
	}

	public void updateInfo(double s) {

		double m = numplus + numminus;
		updateInfo(s, m);
	}

	public void updateInfo(double s, double m) {
		double u = 1;
		if (m != 0 && m > minInteractions)
			u = 1 - (1 / m);

		this.alfa = u * alfa + s;
		this.beta = u * beta + (1 - s);
	}

	public double expectation() {
		return (alfa) / (alfa + beta);
	}

        public void updateRep(double interacions) {

		double bayesianDecRate = 1;
		double m = interacions;

		if (m != 0 && m > minInteractions)
			bayesianDecRate = 1 - (1 / m);

		this.alfa = this.alfa * bayesianDecRate;
		this.beta = this.beta * bayesianDecRate;
	}
	public void updateRep() {

		double bayesianDecRate = 1;
		double m = numplus + numminus;

		if (m != 0 && m > minInteractions)
			bayesianDecRate = 1 - (1 / m);

		this.alfa = this.alfa * bayesianDecRate;
		this.beta = this.beta * bayesianDecRate;
	}

	public double getAlfa() {
		return alfa;
	}

	public void setAlfa(double alfa) {
		this.alfa = alfa;
	}

	public double getBeta() {
		return beta;
	}

	public void setBeta(double beta) {
		this.beta = beta;
	}

	public static double goodObsValue() {
		return 1;
	}

	public static double badObsValue() {
		return 0;
	}
}
